package com.example.jpa.controller;

import com.example.jpa.entity.req.UpdateReq;
import com.example.jpa.repository.StudentInfRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: qilongxin
 * @date: 2021/8/26 19:05
 */
@RestController
public class QueryDslController {
    @Resource
    private ApplicationContext applicationContext;
    @Resource
    private StudentInfRepository studentInfRepository;

    @PutMapping("/update")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public String update(@RequestBody UpdateReq updateReq) {
        Integer result = studentInfRepository.updateStudent(updateReq.getName(), updateReq.getAge());
        applicationContext.publishEvent(result);
        return String.valueOf(result);
    }

}
